package com.vic.backupx.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 发布确认 配置类
 */
@Configuration
public class ConfirmConfig {

    // 交换机
    public static final String CONFIRM_EXCHANGE = "confirm_exchange";
    // 错误交换机
    public static final String CONFIRM_ERROR_EXCHANGE = "confirm_error_exchange";
    // 队列
    public static final String CONFIRM_QUEUE = "confirm_queue";
    // 路由键
    public static final String CONFIRM_ROUTING_KEY = "confirm_rk";
    // 错误路由键
    public static final String CONFIRM_ERROR_ROUTING_KEY = "confirm_error_rk";

    // 声明交换机
    @Bean("confirm_exchange")
    public DirectExchange confirmExchange() {
        // 当 confirm_exchange 无法将消息路由到 confirm_queue 时，将消息传递给 backup_exchange (alternate:替代者、代理人)
        return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE)
                .durable(true).alternate(BackupExchangeConfig.BACKUP_EXCHANGE).build();
    }

    // 声明队列
    @Bean("confirm_queue")
    public Queue delayedQueue() {
        return QueueBuilder.durable(CONFIRM_QUEUE).build();
    }

    // 绑定
    @Bean
    public Binding queueBindingExchange(@Qualifier("confirm_queue") Queue queue, @Qualifier("confirm_exchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(CONFIRM_ROUTING_KEY);
    }

}